home *** CD-ROM | disk | FTP | other *** search
/ TPUG - Toronto PET Users Group / TPUG Users Group CD / TPUG Users Group CD.iso / AMIGA / AMICUS / AMICUS14.ADF / Oings / Oing / oing.c < prev    next >
C/C++ Source or Header  |  1989-01-28  |  6KB  |  192 lines

  1. /*  OING.C                                                  From: AMIGA.4340
  2.  *
  3.  * If you liked "Boing!", there's a better than even chance you'll like this.
  4.  *
  5.  * Leo L. Schwab                8608.6
  6.  */
  7.  
  8. #include <exec/types.h>
  9. #include <exec/memory.h>
  10. #include <intuition/intuition.h>
  11. #include <graphics/sprite.h>
  12.  
  13. #define REV             0L
  14. #define FRAMES          6
  15. #define SPRHEIGHT       16
  16. #define WORDSPERSPR     (2 * SPRHEIGHT + 4)
  17. #define MAXX            (640-32)
  18. #define MAXY            (200-16)
  19. #define MAGIC1          33
  20. #define MAGIC2          21
  21. #define ever            (;;)
  22.  
  23. extern UWORD    ballmask[], ball0[];
  24. extern void     *OpenLibrary(), *OpenWindow(), *AllocMem(), *GetMsg(),
  25.                 *ViewPortAddress();
  26. extern long     GetSprite(), VBeamPos();
  27.  
  28.  
  29. struct NewWindow windef = {
  30.         0, 15, 300, 10,
  31.         -1, -1,
  32.         CLOSEWINDOW,
  33.         WINDOWCLOSE | WINDOWDEPTH | WINDOWDRAG | ACTIVATE,
  34.         NULL, NULL,
  35.         (UBYTE *) "One moment....",
  36.         NULL, NULL, 0, 0, 0, 0,
  37.         WBENCHSCREEN
  38. };
  39. /*      Doing it this way is faster than saying  (i+offset) % 6  */
  40. UBYTE           idx[] = { 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4 };
  41.  
  42. struct SimpleSprite     spr[FRAMES];
  43. struct Window   *win;
  44. struct ViewPort *vp;
  45. UWORD           *sprbuf;
  46. UWORD           *sprites[FRAMES];
  47. int             vx[FRAMES], vy[FRAMES];
  48. void            *GfxBase, *IntuitionBase;
  49.  
  50.  
  51. main ()
  52. {
  53.         int i, offset = 0, flag = 0;
  54.         void *msg;
  55.  
  56.         openstuff ();
  57.         setupsprites ();
  58.         rnd ((short) -VBeamPos());      /*  Plant a seed  */
  59.  
  60.         for (i=0; i<FRAMES; i++) {
  61.                 if (GetSprite (&spr[i], (long) i+2) < 0)
  62.                         die ("Sprite allocation failed.");
  63.                 spr[i].x = 640 / 2;
  64.                 spr[i].y = 10;
  65.                 spr[i].height = SPRHEIGHT;
  66.                 ChangeSprite (vp, &spr[i], sprites[i]);
  67.         }
  68.  
  69.         SetWindowTitles (win, "  \253\253<< Oing! >>\273\273  ", "Oing!");
  70.         for ever {
  71.                 /*
  72.                  * WaitTOF()'s presence way up here is significant.  I used
  73.                  * have it down by the ChangeSprite() loop, but it flickered.
  74.                  * When it's up here, I get just the right amount of delay
  75.                  * after top-of-frame so that nothing glitches.
  76.                  */
  77.                 WaitTOF ();
  78.                 if (msg = GetMsg (win -> UserPort)) {
  79.                         ReplyMsg (msg);
  80.                         closestuff ();
  81.                         return;
  82.                 }
  83.                 for (i=0; i<FRAMES; i++) {
  84.                         spr[i].x += vx[i];
  85.                         if (spr[i].x > MAXX) {
  86.                                 /*  spr.x is unsigned, so we have to cheat  */
  87.                                 spr[i].x = (spr[i].x & 0x8000) ? 0 : MAXX;
  88.                                 vx[i] = -vx[i];
  89.                         }
  90.  
  91.                         /* The shift by 2 is to slow vertical motion a bit */
  92.                         if ((spr[i].y += (vy[i] >> 2)) > MAXY) {
  93.                                 spr[i].y = MAXY;
  94.                                 vx[i] = rnd (MAGIC2) - MAGIC2 / 2;
  95.                                 vy[i] = -rnd (MAGIC1) - 5;
  96.                         }
  97.                         vy[i]++;        /*  Gravity  */
  98.                 }
  99.  
  100.                 for (i=0; i<FRAMES; i++) 
  101.                         ChangeSprite (vp, &spr[i], sprites[idx[i+offset]]);
  102.  
  103.                 /*  Rotate balls every other loop  */
  104.                 if (flag = !flag)
  105.                         offset = (offset + 1) % 6;
  106.         }
  107. }
  108.  
  109. openstuff ()
  110. {
  111.         register int i;
  112.  
  113.         if (!(IntuitionBase = OpenLibrary ("intuition.library", REV)))
  114.                 die ("Intuition open failed.");
  115.  
  116.         if (!(GfxBase = OpenLibrary ("graphics.library", REV)))
  117.                 die ("Art shop closed.");
  118.  
  119.         if (!(win = OpenWindow (&windef)))
  120.                 die ("Window painted shut.");
  121.         vp = ViewPortAddress (win);
  122.  
  123.         for (i=20; i<32; i += 4) {
  124.                 SetRGB4 (vp, i+1L, 15L, 0L, 0L);
  125.                 SetRGB4 (vp, i+3L, 15L, 14L, 13L);
  126.         }
  127. }
  128.  
  129. closestuff ()
  130. {
  131.         register int i;
  132.  
  133.         for (i=0; i<FRAMES; i++)
  134.                 if (spr[i].num)
  135.                         FreeSprite ((long) spr[i].num);
  136.  
  137.         if (sprbuf)
  138.                 FreeMem (sprbuf, 2L * WORDSPERSPR * FRAMES);
  139.         if (win)
  140.                 CloseWindow (win);
  141.         if (GfxBase)
  142.                 CloseLibrary (GfxBase);
  143.         if (IntuitionBase)
  144.                 CloseLibrary (IntuitionBase);
  145. }
  146.  
  147. die (str)
  148. char *str;
  149. {
  150.         if (win) {
  151.                 SetWindowTitles (win, str, -1L);
  152.                 WaitPort (win -> UserPort);
  153.         } else
  154.                 puts (str);
  155.         closestuff ();
  156.         exit (100);
  157. }
  158.  
  159. /*
  160.  * The following code segment was lifted (nearly) intact from the Alpha-9
  161.  * 1.2 README disk.  Thanks to Jim Mackraz for the code, and also for
  162.  * drawing all those ball sprites.
  163.  */
  164. setupsprites ()
  165. {
  166.         UWORD *cw;      /* current position in buffer of sprite images */
  167.         UWORD *maskp;   /* current position in ballmask             */
  168.         UWORD *ballp;   /* current position in ball0 data           */
  169.         int frame, scan;
  170.  
  171.         if (!(sprbuf = AllocMem (2L * WORDSPERSPR * FRAMES, MEMF_CHIP)))
  172.                 die ("Can't allocate sprite buffer.");
  173.  
  174.         cw = sprbuf;    /* current position at top of buffer */
  175.         ballp = ball0;  /* ... top of data to be interleaved */
  176.  
  177.         for (frame=0; frame<FRAMES; frame++) {
  178.                 maskp = ballmask;       /* one mask for all frames */
  179.                 sprites[frame] = cw;
  180.                 *cw++ = 0;              /* poscntl */
  181.                 *cw++ = 0;
  182.  
  183.                 /* one word from ball0, one word from ballmask */
  184.                 for (scan=0; scan<SPRHEIGHT; scan++) {
  185.                         *cw++ = *maskp++;
  186.                         *cw++ = *ballp++;
  187.                 }
  188.                 *cw++ = 0;      /* termination */
  189.                 *cw++ = 0;
  190.         }
  191. }
  192.